import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [

    {
      path:'/',
      name:'login',
      component:()=> import('../views/Login.vue')
    },
    {
      path:'/home',
      name:'home',
      component:()=> import('../views/home.vue')
    }
    // {
    //   path: '/about',
    //   name: 'about',
    //   // route level code-splitting
    //   // this generates a separate chunk (About.[hash].js) for this route
    //   // which is lazy-loaded when the route is visited.
    //   component: () => import('../views/AboutView.vue')
    // }
  ]
})
router.beforeEach((to,from,next)=>{
  //检查用户是否登录
  const isLogin = localStorage.token ? true : false
  if(!isLogin){
    if(to.path !=='/'){
      //重定向到根目录
      next('/')
    }else{
      next()
    }
  }else{
    //如果已经登录就正常进入
    next()
  }
})
export default router
